package com.atm.simulation;

import java.util.Scanner;
import com.atm.domain.ATM;
import com.atm.domain.Card;
import com.atm.simulation.bankSim.Account;
import com.atm.simulation.bankSim.AccountList;

public class mainofATM {
	private static Account account;
	private static AccountList accountList = new AccountList();

	private static int count; // 记录输入密码的次数

	public static void main(String[] args) {
		ATM atm = new ATM();
		atm.turnOn();
		Scanner sc = new Scanner(System.in);
		pluginCard(atm, sc);
	}

	public static void pluginCard(ATM atm, Scanner sc) {
		// 提示
		System.out.println("------------银行后台数据-----------");
		System.out.println("卡号---------密码----------余额----------");
		System.out.println("0001" + "-" + 1234 + "----------" + "2000");
		System.out.println("0002" + "-" + 1234 + "----------" + "5000");
		System.out.println("0003" + "-" + 1234 + "----------" + "4500");

		count = 0;

		System.out.print("请输入卡号:\n");
		Card card = atm.pluginCard(sc.nextLine());
		account = accountList.getAccountByCardNo(card.getCardNo());
		choose(atm,card,sc);
	}
	
	public static void choose(ATM atm, Card card, Scanner sc) {
		int select = 0;
		System.out.print("选择：1.输入密码；2.取卡：\n");
		switch (Integer.parseInt(sc.nextLine())) {
		case 1:
			verification(atm, card, sc);
			break;
		case 2:
			atm.dropOut(card);
			pluginCard(atm, sc);
			break;
		default:
			break;
		}

	}
	
	public static void verification(ATM atm, Card card, Scanner sc) {

		System.out.print("请输入密码：\n");
		if (atm.getSessionAfterIdentify(card,sc.nextLine()) != null) {
			// 密码正确
			selectBusiness(atm, card, sc);
		} else {
			System.out.println("密码错误！请重新输入（连续输错3次将锁定银行卡）。");
			count += 1;
			if (count >= 3) {
				atm.swallowCard(card);
			} else {
				choose(atm, card, sc);
			}
		}
	}

	public static void selectBusiness(ATM atm, Card card, Scanner sc) {
		System.out.print("账号：" + card.getCardNo() + "登录成功，请选择办理业务：1为取款，2为存款，3为转账，4为查询，5为退卡：\n");
		
		int businessNo = Integer.parseInt(sc.nextLine());
		transaction(atm, card, sc, businessNo);
	}
	
	public static void transaction(ATM atm, Card card, Scanner sc, int businessNo) {
		switch (businessNo) {
		case 1:
			System.out.println("账户余额：" + account.getAmount());
			System.out.print("请输入取款金额(为100的倍数)：\n");
			
			int amount = Integer.parseInt(sc.nextLine());
			if (amount % 100 != 0) {
				System.out.println("取款金额要为100的倍数，请重新输入");
				transaction(atm, card, sc, 1);
			} else {
				// boolean withdrawalStatus = atm.withdrawal(card, amount);

				if (atm.withdrawal(card, amount)) {
					account.setAmount(account.getAmount() - amount); // 处理余额显示

					// 出钞，拿走现金

					atm.getCash(amount);
					String a = (String)sc.nextLine();
					if (a.equalsIgnoreCase("1") || a.equalsIgnoreCase("2")) {
						System.out.print("选择：1.继续操作；2.取卡：\n");
						String ass = (String)sc.nextLine();
						int isContinue = Integer.parseInt(ass);
						if (isContinue == 1) {
							selectBusiness(atm, card, sc);
						} else if (isContinue == 2) {
							System.out.println("业务结束！");
						}
					} else {
						System.out.println("输入格式错误！");
					}
					// 判断是否打印凭条

					String b = (String)sc.nextLine();
					System.out.print("是否打印凭条（1打印，2结束业务）：");

					if (b.equalsIgnoreCase("1") || b.equalsIgnoreCase("2")) {
						int isPrint = Integer.parseInt(b);
						if (isPrint == 1) {
							atm.printReceipt(card, amount);

							System.out.println("打印成功！");
							selectBusiness(atm, card, sc);
						} else if (isPrint == 2) {
							System.out.println("业务结束！");
							selectBusiness(atm, card, sc);

						}
					} else {
						System.out.println("输入格式错误！");
					}
				}
			}
			break;
		case 5:
			atm.dropOut(card);
			pluginCard(atm, sc);
			break;
		default:
			System.out.println("该业务暂未开通！");
			selectBusiness(atm, card, sc);
			break;
		}
	}
	
}
